//http://saas-app-test.ybveg.com/
//http://api.tw-soft.com/
const url = 'http://api.tw-soft.com/'
//const url2 = 'http://saas-app-test.ybveg.com/'
const app = getApp();
const common = require('../../utils/common')
const api = require('../../config/api');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		cheakall: false,
		fee: [],
		payrecords: [],
		wuye: [],
		all: 0,
		array: [],
		list: [],
		index: 0,
		items: ['未缴账单', '已缴账单'],
		current: 0,
		nodata: false,
		modalName: '',
		TabCur: 0,
		scrollLeft: 0,
		mobile:''
	},
	tabSelect(e) {
		const id = e.currentTarget.dataset.id;
		if (id == 1) {
			this.gethistorybill();
		}
		this.setData({
			TabCur: id,
			scrollLeft: (id - 1) * 120
		})
	}, //全选
	getphone(){
		const that=this;
		common.request(api.wuyephone, 'get').then(res => {
			console.log(res.data.mobile)
			that.getload(res.data.mobile)
		})
	},
	getload(mobile){
		const that=this;
		wx.request({
			url: url + '2094/Comm/GetRoomListByMobile',
			data: {
				mobile
			},
			success(res) {
				const data = res.data.data;
				that.setData({
					list: data,
					array: data.map(item => {
						return item.RoomName;
					})
				});
				that.getpay(data, that.data.index)
			}
		})
	},
	gethistorybill() {
		const data = this.data.list;
		const i = this.data.index;
		const that = this;
		common.request(api.wuyeList, {
			pageNo: 1
		}, 'post').then(res => {
			this.setData({
				payrecords: res.data.dataList
			})
		})
		// wx.request({
		// 	url:url+'2094/Comm/GetBillListById',
		// 	data:{
		// 		roomid:data[i].RoomID,
		// 		custid:data[i].CustID
		// 	},
		// 	success(res) {

		// 		that.setData({
		// 			payrecords:res.data.data
		// 		})
		// 	}
		// })
	},
	checkboxChange(e) {
		const ch = e.detail.value;
		const data = this.data.wuye;
		let all = 0;
		if (ch.length > 0) {

			data.forEach(item => {
				item.isSelected = true;
				all += parseInt(item.LateFeeAmount) + parseInt(item.DebtsAmount);
			})

		} else {
			data.forEach(item => {
				item.isSelected = false;
			})
			this.setData({
				all: 0
			})
		}
		this.setData({
			wuye: data,
			all
		})
	},

	bindPickerChange(e) {

		this.setData({
			cheakall: false,
			all: 0
		})
		console.log(this.data.cheakall);
		const i = e.detail.value;
		this.setData({
			index: i
		});
		this.getpay(this.data.list, i);

	},
	radioChange(e) {
		const i = e.detail.value;
		if (i.length > 0) {
			this.setData({
				all: i.reduce((pre, next) => {
					return parseInt(pre) + parseInt(next)
				}),
				fee: i
			})
		} else {
			this.setData({
				all: 0,
				fee: []
			})
		}

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getphone()
	},

	getpay(data, i) {
		const that = this;
		wx.request({
			url: url + '2094/Comm/GetFeesListById',
			data: {
				roomid: data[i].RoomID,
				custid: data[i].CustID
			},
			success(res) {
				const data = res.data.data;

				data.forEach((item) => {
					item.money = parseInt(item.LateFeeAmount) + parseInt(item.DebtsAmount);
					item.isSelected = false;
				})
				// data.push({
				// 	money:0.1,
				// 	isSelected:false
				// })
				that.setData({
					wuye: data
				})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},

	showModal(e) {
		if (this.data.fee.length > 0) {
			this.setData({
				modalName: 'bottomModal'
			})
		} else {
			console.log(1111)
			wx.showModal({
				content: '请选择账单！'
			})
		}

	},
	hideModal(e) {
		this.setData({
			modalName: null
		})
	},
	pay() {
		const arr = this.data.list[this.data.index];

		const data = {
			amount: this.data.all,
			custid: arr.CustID,
			roomid: arr.RoomID
		}
		common.request(api.RechargeInfo, data, 'POST').then(function(data) {
			if (data.type == 'success') {
				wx.requestPayment({
					timeStamp: data.data.timeStamp,
					nonceStr: data.data.nonceStr,
					package: data.data.package,
					signType: data.data.signType,
					paySign: data.data.paySign,
					success: function(res) {
						common.showSuccessToast('充值成功');
						setTimeout(function() {
							wx.navigateBack();
						}, 1500)
						isOperate = 0;
					},
					fail: function(res) {
						common.showErrorToast('充值失败');
						isOperate = 0;
					}
				})
			} else {
				common.showErrorToast(data.content);
				isOperate = 0;
			}
		})

		// wx.navigateTo({
		// 	url: '/pages/paySuccess/index',
		// })
	}
})
